package com.qfedu.mtl.controller;

import com.alibaba.fastjson.JSON;
import com.qfedu.mtl.entity.Menus;
import com.qfedu.mtl.query.MenusQuery;
import com.qfedu.mtl.service.MenusService;
import com.qfedu.mtl.service.impl.MenusServiceImpl;
import com.qfedu.mtl.util.CharacterUtil;
import com.qfedu.mtl.util.HttpUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.web.annotation.Controller;
import com.qfedu.mtl.util.web.annotation.RequestMapping;
import com.qfedu.mtl.vo.TableVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @version v1.0
 * @Date 2023/9/28 11:50
 * @Author DARKER
 * @Email 1402216045@qq.com
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    MenusService menusService = new MenusServiceImpl();
    //处理渲染菜单的请求接口
    @RequestMapping("/list.do")
    public void menusList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        String page = StrUtil.ifNull(request.getParameter("page"), "1");
        String size = StrUtil.ifNull(request.getParameter("size"), "10");
        MenusQuery menusQuery = HttpUtil.request2Bean(request, MenusQuery.class);
        //业务层返回查询到的菜单
        TableVO<Menus> vo = menusService.findMenusList(Integer.valueOf(page),Integer.valueOf(size),menusQuery);
        //返回给前端
        String json = JSON.toJSONString(vo);
        response.getWriter().println(json);
    }

    //处理渲染菜单的请求接口
    @RequestMapping("/level.do")
    public void menuLevel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        List<Integer> vo = menusService.findLevel();
        //业务层返回查询到的菜单

        //返回给前端
        String json = JSON.toJSONString(vo);
        response.getWriter().println(json);
    }

    //处理渲染菜单的请求接口
    @RequestMapping("/parentCode.do")
    public void parentMenuCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        List<String> vo = menusService.findParentMenuCode();
        //业务层返回查询到的菜单

        //返回给前端
        String json = JSON.toJSONString(vo);
        response.getWriter().println(json);
    }
    //处理批量删除的请求接口
    @RequestMapping("/delBatch.do")
    public void delBatchById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        String id = request.getParameter("id");  //  id  =》1,2,3
        //卫语句,防止前端传入空的id
        if (StrUtil.isNullOrEmpty(id)){
            HttpUtil.response(null,-1,"请选择要删除的id",response);
            return;
        }
        //分割
        String[] idss = id.split(",");
        //转为整型集合
        List<Integer> ids = Stream.of(idss).map(e -> Integer.valueOf(e)).collect(Collectors.toList());
        //尝试批量删除
        try {
            menusService.delBatchById(ids);
            HttpUtil.responseSuccess(null,response);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),response);
        }
    }


}
